package com.avic.rong.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.bean.HomeBean;
import com.avic.rong.bean.show_bean.PingBean;
import com.avic.rong.questionOrAnswer.activity.PersonalHomeActivity;
import com.avic.rong.utils.DateUtils;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.example.minato.minastore.utils.GlideApp;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 评论
 * Creater: Created by wangyz on 2017/1/10.
 * Email: 936804097@qq.com
 */

public class CommandAdapter extends RecyclerView.Adapter<CommandAdapter.CommandViewHolder> {


    private Context mContext;
    private List<PingBean.CommentsBean> data = new ArrayList<>();

    public CommandAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<PingBean.CommentsBean> list) {
        this.data = list;
        notifyDataSetChanged();
    }

    @Override
    public CommandViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_command, parent, false);

        CommandViewHolder holder = new CommandViewHolder(view, viewType);

        return holder;
    }

    @Override
    public void onBindViewHolder(CommandViewHolder holder, final int position) {


        GlideApp.with(mContext).load(data.get(position).getLogo())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .error(R.mipmap.deafult_image_header)
                .into(holder.imgCategory);

        if (data.get(position).getIsV()==1) {
            holder.pinglun_daV.setVisibility(View.VISIBLE);
        }
        holder.tvCategoryName.setText(data.get(position).getContent());
        long nowtime = System.currentTimeMillis();
        long addTime = data.get(position).getAddTime();
        String distanceTime = DateUtils.getDistanceTime(addTime, nowtime);
        holder.tvTip.setText(distanceTime + "");
        holder.tvContent.setText(data.get(position).getUserName());

        holder.imgCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object userId = data.get(position).getUserId();
                String userIds = userId.toString();
                double userIdDou = Double.parseDouble(userIds);
                int userIdsO = (int) userIdDou;
                Intent PresenIntent = new Intent(mContext, PersonalHomeActivity.class);
                PresenIntent.putExtra("homeUserID", userIdsO + "");
                mContext.startActivity(PresenIntent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return data.size();
    }


    class EmptyBitmapListener implements RequestListener<Drawable> {
        private CommandViewHolder holder;
        private HomeBean.CriterionBean recommand;

        public EmptyBitmapListener(CommandViewHolder holder, HomeBean.CriterionBean recommand) {
            this.holder = holder;
            this.recommand = recommand;
        }

        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {

            if (!TextUtils.isEmpty(recommand.getTargetCatName())) {
                holder.tv_content.setVisibility(View.VISIBLE);
                holder.tv_content.setText(recommand.getTargetCatName());
            }
            return false;
        }

        @Override
        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
            return false;
        }
    }


    class CommandViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_category)
        ImageView imgCategory;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_category_name)
        TextView tvCategoryName;

        @BindView(R.id.tv_tip)
        TextView tvTip;
        TextView tv_content;//无图提示
        private final ImageView pinglun_daV;


        public CommandViewHolder(View itemView, int type) {
            super(itemView);
            pinglun_daV = itemView.findViewById(R.id.pinglun_daV);
            ButterKnife.bind(this, itemView);
        }
    }



}
